<?php

require 'orm/om/BaseTeamPeer.php';


/**
 * Skeleton subclass for performing query and update operations on the 'team' table.
 *
 *
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package    orm
 */
class TeamPeer extends BaseTeamPeer {


	// function to get all the player associated with the team
	public static function getPlayers($teamId){

		$criteria = new Criteria();

		$criteria->add(PlayerToTeamPeer::TEAM_ID, $teamId);

		$oTeamToPlayers = PlayerToTeamPeer::doSelectJoinPlayer($criteria);
		//print_r($oTeamToPlayers);
		foreach ($oTeamToPlayers as $oTeamPlayer){

			$oPlayers[] = $oTeamPlayer->getPlayer();

		}
		return $oPlayers;
	}

	// function to get all the active players in current team
	public static function getActivePlayers($teamId){

		$criteria = new Criteria();

		$criteria->add(PlayerToTeamPeer::TEAM_ID, $teamId);

		$oTeamToPlayers = PlayerToTeamPeer::doSelectJoinPlayer($criteria);

		foreach ($oTeamToPlayers as $oTeamPlayer){

			$player = $oTeamPlayer->getPlayer();
			if($player->getIsActive() == 'Y'){
				$oPlayers[] = $player;
			}


		}

		return $oPlayers;
	}


	public static function getTeams()
	{
		$criteria =  new Criteria();
		$oTeams = TeamPeer::doSelect($criteria);
		foreach($oTeams as $team){
			$aTeams[$team->getId()] = $team->getName();
		}
		return $aTeams;


	}

	// function to fetch the upcoming matches of the team
	public static function getUpcomingMatches($teamId){

		$criteria = new Criteria();
		$crit_1 = $criteria->getNewCriterion(MatchSchedulePeer::FIRST_TEAM, $teamId);
		$crit_2 = $criteria->getNewCriterion(MatchSchedulePeer::SECOND_TEAM, $teamId);
		$crit_3 = $criteria->getNewCriterion(MatchSchedulePeer::IS_PUBLISED, 'N');

		$crit_1->addOr($crit_2);

		$crit_1->addAnd($crit_3);

		$criteria->add($crit_1);

		$matches = MatchSchedulePeer::doSelect($criteria);

		if($matches){
			return $matches;
		}else{
			return null;
		}

	}

	public static function getTournaments($teamId){

		$oTeam = TeamPeer::retrieveByPK($teamId);


		$criteria = new Criteria();

		$criteria->add(TournamentPeer::IS_ACTIVE, 'Y');

		$oTournaments = $oTeam->getTournamentToTeamsJoinTournament($criteria);
		$tournament = array();
		if($oTournaments){
			foreach($oTournaments as $series){
				$tournament[] = $series->getTournament();
			}

		}
		return $tournament;


	}

	public static function getTeamName($id){

		$oTeam = TeamPeer::retrieveByPK($id);
		return $oTeam->getName();
	}

} // TeamPeer
